import hashlib
import tkinter as tk
from tkinter import messagebox

def generate_key(secret_key):
    return hashlib.sha256(secret_key.encode()).hexdigest()

def encrypt(plain_text, secret_key):
    key = generate_key(secret_key)
    plain_text_bytes = plain_text.encode()
    key_bytes = bytes.fromhex(key[:32])  # 只取前32位
    cipher_text = bytearray()
    for i, byte in enumerate(plain_text_bytes):
        cipher_text.append(byte ^ key_bytes[i % len(key_bytes)])
    return cipher_text.hex()

def decrypt(cipher_text, secret_key):
    key = generate_key(secret_key)
    cipher_text_bytes = bytes.fromhex(cipher_text)
    key_bytes = bytes.fromhex(key[:32])  # 只取前32位
    plain_text = bytearray()
    for i, byte in enumerate(cipher_text_bytes):
        plain_text.append(byte ^ key_bytes[i % len(key_bytes)])
    return plain_text.decode()

def on_encrypt():
    plain_text = entry_plain.get(1.0, tk.END).strip()  # 获取整个文本框的内容并去除两端空白字符
    secret_key = entry_key.get()
    cipher_text = encrypt(plain_text, secret_key)
    text_result.delete(1.0, tk.END)
    text_result.insert(tk.END, cipher_text)

def on_decrypt():
    cipher_text = entry_cipher.get(1.0, tk.END).strip()  # 获取整个文本框的内容并去除两端空白字符
    secret_key = entry_key_decrypt.get()
    try:
        plain_text = decrypt(cipher_text, secret_key)
        text_result.delete(1.0, tk.END)
        text_result.insert(tk.END, plain_text)
    except ValueError as e:
        messagebox.showerror("解密失败", f"解密失败：{e}")

def on_run():
    choice = var.get()
    if choice == '1':
        frame_encrypt.pack(fill=tk.BOTH, expand=True)
        frame_decrypt.pack_forget()
    elif choice == '2':
        frame_encrypt.pack_forget()
        frame_decrypt.pack(fill=tk.BOTH, expand=True)

# 创建主窗口
root = tk.Tk()
root.title("加密/解密工具")
root.geometry("800x400")  # 设置窗口大小

# 创建变量
var = tk.StringVar(root)
var.set('1')  # 默认选择加密

# 创建加密和解密的框架
frame_left = tk.Frame(root, width=400, height=380)
frame_left.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)

frame_encrypt = tk.Frame(frame_left)
frame_decrypt = tk.Frame(frame_left)

# 创建加密部分的控件
label_plain = tk.Label(frame_encrypt, text="输入需要加密的内容：")
label_plain.pack(pady=5)
entry_plain = tk.Text(frame_encrypt, width=50, height=10)
entry_plain.pack(pady=5)
entry_key = tk.Entry(frame_encrypt, width=50)
entry_key.pack(pady=5)
label_key = tk.Label(frame_encrypt, text="输入口令：")
label_key.pack(pady=5)
button_encrypt = tk.Button(frame_encrypt, text="加密", command=on_encrypt)
button_encrypt.pack(pady=5)

# 创建解密部分的控件
label_cipher = tk.Label(frame_decrypt, text="输入需要解密的内容：")
label_cipher.pack(pady=5)
entry_cipher = tk.Text(frame_decrypt, width=50, height=10)
entry_cipher.pack(pady=5)
entry_key_decrypt = tk.Entry(frame_decrypt, width=50)
entry_key_decrypt.pack(pady=5)
label_key_decrypt = tk.Label(frame_decrypt, text="输入口令：")
label_key_decrypt.pack(pady=5)
button_decrypt = tk.Button(frame_decrypt, text="解密", command=on_decrypt)
button_decrypt.pack(pady=5)

# 创建结果显示区域
text_result = tk.Text(root, height=10, width=40)
text_result.pack(side=tk.RIGHT, fill=tk.BOTH, expand=True)

# 创建选择操作的控件
label_choice = tk.Label(root, text="请选择操作：")
label_choice.pack(pady=5)
radio_encrypt = tk.Radiobutton(root, text="加密", variable=var, value='1', command=on_run)
radio_encrypt.pack(side=tk.LEFT, anchor=tk.W)
radio_decrypt = tk.Radiobutton(root, text="解密", variable=var, value='2', command=on_run)
radio_decrypt.pack(side=tk.LEFT, anchor=tk.W)

# 设置默认显示加密界面
on_run()

# 运行主循环
root.mainloop()